package bb.lanxing.util.img;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.github.chrisbanes.photoview.PhotoView;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.util.image.GlideUtils;
import java.util.ArrayList;
import java.util.List;


public class ImageChooseDetailActivity extends BaseActivity {
    public static final String LOCAL_FILE_INDEX = "local_file_index";
    public static final String LOCAL_FILE_LIST = "local_files";
    public static final String LOCAL_FILE_MAX = "local_file_max";
    CheckBox checkBox;
    private List<LocalFile> localFiles;
    private int max;
    Button okBtn;
    private final CompoundButton.OnCheckedChangeListener onCheckedChangeListener = (compoundButton, z) -> {
        ArrayList<LocalFile> checkedItems = LocalImageHelper.getInstance().getCheckedItems();
        if (z) {
            if (checkedItems.size() < ImageChooseDetailActivity.this.max) {
                checkedItems.add((LocalFile) ImageChooseDetailActivity.this.localFiles.get(ImageChooseDetailActivity.this.viewPager.getCurrentItem()));
            } else {
                ImageChooseDetailActivity.this.checkBox.setOnCheckedChangeListener(null);
                ImageChooseDetailActivity.this.checkBox.setChecked(false);
                ImageChooseDetailActivity.this.checkBox.setOnCheckedChangeListener(ImageChooseDetailActivity.this.onCheckedChangeListener);
                ImageChooseDetailActivity imageChooseDetailActivity = ImageChooseDetailActivity.this;
                Toast.makeText(imageChooseDetailActivity, imageChooseDetailActivity.getString(R.string.toast_image_chooser_max, imageChooseDetailActivity.max), Toast.LENGTH_SHORT).show();
            }
        } else {
            checkedItems.remove(ImageChooseDetailActivity.this.localFiles.get(ImageChooseDetailActivity.this.viewPager.getCurrentItem()));
        }
        ImageChooseDetailActivity.this.refreshFinishState();
    };
    Toolbar toolbar;
    ViewPager viewPager;

    @Override 
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_image_choose_detail);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        this.viewPager = (ViewPager) findViewById(R.id.image_chooser_detail_pager);
        this.checkBox = (CheckBox) findViewById(R.id.image_chooser_detail_choose);
        this.okBtn = (Button) findViewById(R.id.image_chooser_detail_ok);
        setSupportActionBar(this.toolbar);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayHomeAsUpEnabled(true);
            supportActionBar.setDisplayShowTitleEnabled(true);
        }
        String stringExtra = getIntent().getStringExtra(LOCAL_FILE_LIST);
        this.max = getIntent().getIntExtra(LOCAL_FILE_MAX, -1);
        if (stringExtra == null) {
            return;
        }
        this.localFiles = LocalImageHelper.getInstance().getFolder(stringExtra);
        this.viewPager.setAdapter(new ImageAdapter());
        int intExtra = getIntent().getIntExtra(LOCAL_FILE_INDEX, 0);
        this.viewPager.setCurrentItem(intExtra);
        this.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
            @Override
            public void onPageScrollStateChanged(int i) {
            }

            @Override
            public void onPageScrolled(int i, float f, int i2) {
            }

            @Override
            public void onPageSelected(int i) {
                ImageChooseDetailActivity.this.checkButton(i);
            }
        });
        if (this.max > 1) {
            this.checkBox.setOnCheckedChangeListener(this.onCheckedChangeListener);
        } else {
            this.checkBox.setVisibility(View.INVISIBLE);
        }
        this.okBtn.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) {
                if (ImageChooseDetailActivity.this.max == 1) {
                    LocalImageHelper.getInstance().getCheckedItems().add((LocalFile) ImageChooseDetailActivity.this.localFiles.get(ImageChooseDetailActivity.this.viewPager.getCurrentItem()));
                }
                ImageChooseDetailActivity.this.setResult(-1);
                ImageChooseDetailActivity.this.finish();
            }
        });
        checkButton(intExtra);
        refreshFinishState();
    }

    public void checkButton(int i) {
        boolean contains = LocalImageHelper.getInstance().getCheckedItems().contains(this.localFiles.get(i));
        this.checkBox.setOnCheckedChangeListener(null);
        this.checkBox.setChecked(contains);
        this.checkBox.setOnCheckedChangeListener(this.onCheckedChangeListener);
    }

    public void refreshFinishState() {
        StringBuilder sb;
        int size;
        ArrayList<LocalFile> checkedItems = LocalImageHelper.getInstance().getCheckedItems();
        if (checkedItems.isEmpty()) {
            this.okBtn.setText(getString(R.string.ok));
        } else {
            if (this.max > 0) {
                sb = new StringBuilder();
                sb.append(checkedItems.size());
                sb.append("/");
                size = this.max;
            } else {
                sb = new StringBuilder();
                size = checkedItems.size();
            }
            sb.append(size);
            sb.append(" ");
            String sb2 = sb.toString();
            this.okBtn.setText(sb2 + getString(R.string.ok));
        }
        if (this.max == 1) {
            if (checkedItems.size() != 1) {
                return;
            }
            setResult(-1);
            finish();
            return;
        }
        this.okBtn.setEnabled(!checkedItems.isEmpty());
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem menuItem) {
        if (menuItem.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(menuItem);
    }

    class ImageAdapter extends PagerAdapter {
        ImageAdapter() {
        }

        @Override 
        public Object instantiateItem(ViewGroup viewGroup, int i) {
            PhotoView photoView = new PhotoView(ImageChooseDetailActivity.this);
            photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            GlideUtils.Companion companion = GlideUtils.companion;
            ImageChooseDetailActivity imageChooseDetailActivity = ImageChooseDetailActivity.this;
            companion.loadImage(imageChooseDetailActivity,
                    ((LocalFile) imageChooseDetailActivity.localFiles.get(i)).getUri().toString(),
                    photoView, R.drawable.bg_photo_default, R.drawable.bg_photo_default);
            viewGroup.addView(photoView, -1, -1);
            return photoView;
        }

        @Override 
        public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
            viewGroup.removeView((View) obj);
        }

        @Override 
        public int getCount() {
            return ImageChooseDetailActivity.this.localFiles.size();
        }

        @Override 
        public boolean isViewFromObject(View view, Object obj) {
            return view.equals(obj);
        }
    }
}
